<?php


namespace app\home\severs;


use think\Exception;
use think\facade\Db;

class ProcessReporting extends Base
{
    public static function onSure($param,$uid=0)
    {
        $id=$param['id']??0;
        if (!$id){
            return self::jump(1,'参数缺失');
        }
        $info=Db::connect('company')->name('process_reporting')->where('id',$id)->find();
        if (!$info||$info['status']!=2){
            return self::jump(1,'参数有误');
        }
        Db::connect('company')->startTrans();
        try {
            $readInfo=Db::connect('company')->name('craft_route_detail')->where('id',$info['craft_detail_id'])->find();
            if (!$readInfo){
                throw new Exception('参数有误');
            }
            $rate_data=[];
            if ($info['pricing_mode']=='班组'){
                $teamDetail=Db::connect('company')->name('team_detail')
                    ->where('status',1)->where('team_id',$info['u_g_id'])->field('user_id,allocation_ratio')->select();
                if (!$teamDetail){
                    throw new Exception('请添加班组成员');
                }
                foreach ($teamDetail as $td){
                    $num=$info['qualifiedNum']*$td['allocation_ratio']/100;
                    $rate_data[]=[
                        'report_id'=>$info['id'],
                        'group_id'=>$info['u_g_id'],
                        'uid'=>$td['user_id'],
                        'qualifiedNum'=>$num,
                        'passNum'=>$info['passNum']*$td['allocation_ratio']/100,
                        'wasteNum'=>$info['wasteNum']*$td['allocation_ratio']/100,
                        'plan_id'=>$info['correlation_order'],
                        'road_id'=>$info['craft_detail_id'],
                        'material_dossier_id'=>$info['material_dossier_id'],
                        'price'=>$readInfo['price'],
                        'salary'=>$num*$readInfo['price'],
                        'create_time'=>time(),
                        'update_time'=>time(),
                    ];
                }
            }else{
                $rate_data[]=[
                    'report_id'=>$info['id'],
                    'plan_id'=>$info['correlation_order'],
                    'road_id'=>$info['craft_detail_id'],
                    'uid'=>$info['u_g_id'],
                    'material_dossier_id'=>$info['material_dossier_id'],
                    'price'=>$readInfo['price'],
                    'qualifiedNum'=>$info['qualifiedNum'],
                    'passNum'=>$info['passNum'],
                    'wasteNum'=>$info['wasteNum'],
                    'salary'=>$info['qualifiedNum']*$readInfo['price'],
                    'create_time'=>time(),
                    'update_time'=>time(),
                ];
            }
            Db::connect('company')->name('process_reporting')->where('id',$id)->update([
                'checkUser'=>$uid,
                'status'=>3,
                'update_time'=>time(),
                'checkTime'=>time()]);
            Db::connect('company')->name('piece_rate_detail')->insertAll($rate_data);
            $params=request()->param();
            $params['id']=$id;
            add_log('check', $id,$params,'工序报工',$uid);
            Db::connect('company')->commit();
        }catch (\Exception $e){
            Db::connect('company')->rollback();
            return self::jump(1,'操作失败:'.$e->getMessage());
        }
        return self::jump(0,'操作成功');
}

    public static function onCancel($param,$uid=0)
    {
        $id=$param['id']??0;
        if (!$id){
            return self::jump(1,'参数缺失');
        }
        $info=Db::connect('company')->name('process_reporting')->where('id',$id)->find();
        if (!$info||$info['status']!=3){
            return self::jump(1,'参数有误');
        }
        $mxInfo=Db::connect('company')->name('piece_rate_detail')->where('status','2')->where('report_id',$id)->count();
        if ($mxInfo){
            return self::jump(1,'请撤销复核计件工资明细');
        }
        Db::connect('company')->startTrans();
        try {

            Db::connect('company')->name('process_reporting')->where('id',$id)->update([
                'checkUser'=>$uid,
                'status'=>1,
                'update_time'=>time(),
                'checkTime'=>time()]);
            Db::connect('company')->name('piece_rate_detail')->where('report_id',$id)->update(['status'=>-1,'deleted'=>time(),'update_time'=>time()]);
              add_log('back', $id,$param,'工序报工',$uid);
            Db::connect('company')->commit();
        }catch (\Exception $e){

            Db::connect('company')->rollback();
            return self::jump(1,'操作失败:'.$e->getMessage());
        }
        return self::jump(0,'操作成功');
}
}